package com.have.blog.admin.controller;


import com.have.blog.admin.annotation.operationLogger.OperationLogger;
import com.have.blog.admin.constant.AdminMessageConstant;
import com.have.blog.admin.domain.dto.PictureCategoryDto;
import com.have.blog.admin.service.AdminPictureSortService;
import com.have.blog.base.exception.ThrowableUtils;
import com.have.blog.base.validator.group.Delete;
import com.have.blog.base.validator.group.GetList;
import com.have.blog.base.validator.group.Insert;
import com.have.blog.base.validator.group.Update;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.core.annotion.avoidRepeatableCommit.AvoidRepeatableCommit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(value = "图片分类相关接口", tags = {"图片分类相关接口"})
@RestController
@RequestMapping("/pictureSort")
@Slf4j
public class AdminPictureCategoryController {
    @Autowired
    private AdminPictureSortService pictureSortService;

    @ApiOperation(value = "获取图片分类列表", notes = "获取图片分类列表", response = String.class)
    @PostMapping(value = "/getList")
    public Wrapper<CommonPage> getList(@Validated({GetList.class}) @RequestBody PictureCategoryDto pictureSortDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取图片分类列表: {}， pageNum:{}, pageSize:{}", pictureSortDto, pictureSortDto.getPageNum(), pictureSortDto.getPageSize());
        List<PictureCategoryDto> list = pictureSortService.getList(pictureSortDto);
        return Wrapper.success(CommonPage.restPage(list));
    }


    @AvoidRepeatableCommit
    @OperationLogger(value = "增加图片分类")
    @ApiOperation(value = "增加图片分类", notes = "增加图片分类", response = String.class)
    @PostMapping("/add")
    public Wrapper add(@Validated({Insert.class}) @RequestBody PictureCategoryDto pictureSortDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("增加图片分类: {}", pictureSortDto);
        return pictureSortService.addPictureSort(pictureSortDto);
    }

    @OperationLogger(value = "编辑图片分类")
    @ApiOperation(value = "编辑图片分类", notes = "编辑图片分类", response = String.class)
    @PostMapping("/edit")
    public Wrapper edit(@Validated({Update.class}) @RequestBody PictureCategoryDto pictureSortDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("编辑图片分类: {}", pictureSortDto);
        return pictureSortService.editPictureCategory(pictureSortDto);
    }

    @OperationLogger(value = "删除图片分类")
    @ApiOperation(value = "删除图片分类", notes = "删除图片分类", response = String.class)
    @PostMapping("/delete")
    public Wrapper delete(@Validated({Delete.class}) @RequestBody PictureCategoryDto pictureSortDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("删除图片分类: {}", pictureSortDto);
        return pictureSortService.deletePictureCategory(pictureSortDto);
    }

    @OperationLogger(value = "置顶分类")
    @ApiOperation(value = "置顶分类", notes = "置顶分类", response = String.class)
    @PostMapping("/stick")
    public Wrapper stick(@Validated({Delete.class}) @RequestBody PictureCategoryDto pictureSortDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("置顶图片分类: {}", pictureSortDto);
        return pictureSortService.stickPictureCategory(pictureSortDto);
    }

    @OperationLogger(value = "通过Uid获取分类")
    @ApiOperation(value = "通过Uid获取分类", notes = "通过Uid获取分类", response = String.class)
    @PostMapping("/getPictureSortByUid")
    public Wrapper getPictureSortByUid(@Validated({Delete.class}) @RequestBody PictureCategoryDto pictureSortDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        PictureCategoryDto dto = pictureSortService.getById(pictureSortDto.getUid());
        if (dto == null) {
            return Wrapper.failed(AdminMessageConstant.ENTITY_NOT_EXIST);
        }
        return Wrapper.success(dto);
    }
}
